//部分功能对所有单位开启 k1编辑 2020.7.12 - 2020.12.6

uiSleep 6;
if (!isNil "CRS_blasted_end") exitWith {};
if (isClass(configFile>>"cfgPatches">>"ace_common")) exitWith {};

CRS_blasted_end = {
	_this setUnconscious false;
	_move = switch (currentWeapon _this) do {
		case "": {"amovppnemstpsnonwnondnon"};
		case (primaryWeapon _this): {"amovppnemstpsraswrfldnon"};
		case (secondaryWeapon _this): {"amovpknlmstpsraswlnrdnon"};
		case (handGunWeapon _this): {"amovppnemstpsraswpstdnon"};
		default {"amovppnemstpsnonwnondnon"};
	};
	sleep 2;
	_this playMoveNow _move; //
	[_this, _move] remoteExec ["playMoveNow", 0];
};
CRS_DEBUG_FN = {
	{_idx = _x getVariable "CRS_Fired_eh"; if (!isNil {_idx}) then {_x removeEventHandler ["fired", _idx]; _x setVariable ["CRS_tag_add", nil]}} forEach allUnits + vehicles;
	if (!isNil "CRS_KeyDown") then {(findDisplay 46) displayRemoveEventHandler ["KeyDown", CRS_KeyDown]; sleep .1; call CRS_AddKeyDown};
};
"CRS_debug_eh" addPublicVariableEventHandler {0 spawn CRS_DEBUG_FN}; //除错

if (isServer) then {
	CRS_unconscious_debug = {
		_t = time + 8;
		waitUntil {time > _t || {!alive _this}};
		if (!alive _this) exitWith {};
		_this remoteExec ["CRS_blasted_end", _this];
	};
	
	"CRS_blasted" addPublicVariableEventHandler {(_this select 1) spawn CRS_unconscious_debug};
	"CRS_delete_wh" addPublicVariableEventHandler {(_this select 1) spawn {sleep 120; deleteVehicle _this}};
	"CRS_hide_simulation" addPublicVariableEventHandler {_observer = _this select 1; _observer hideObjectGlobal true; _observer enableSimulationGlobal false}; //
};

if (isDedicated) exitWith {};
waitUntil {!isNull player};

if(isNil "CRS_BackBlast")then {CRS_BackBlast = true}; //尾焰冲击
if(isNil "CRS_TacticalPoint")then {CRS_TacticalPoint = true}; //战术手势
if(isNil "CRS_Fragmentation")then {CRS_Fragmentation = true}; //破片效果
if(isNil "CRS_DisposableAT")then {CRS_DisposableAT = true}; //一次性火箭筒
if(isNil "CRS_STFUCarl")then {CRS_STFUCarl = false}; //个人角色语音


if(isNil "CRS_fragm_type")then {CRS_fragm_type = ["B_127x108_Ball", "B_127x108_APDS", "B_127x108_APDS"]}; //"B_762x51_Ball" "B_338_NM_Ball"
CRS_debug_Stone = ""; //石头 "HandGrenade_Stone"
CRS_NadeList = ["HandGrenade","CUP_HandGrenade_RGO","CUP_HandGrenade_M67","CUP_HandGrenade_RGD5","CUP_HandGrenade_L109A1_HE","CUP_HandGrenade_L109A2_HE","rhs_mag_rgo","rhs_mag_m67","rhs_mag_rgd5","rhssaf_mag_br_m75","rhssaf_mag_br_m84","rhs_mag_f1","rhs_grenade_mkii_mag", CRS_debug_Stone];
CRS_spawn_fragm = {
	params ["_unit", "_nade", "_speedCheck"];
	_nadePos = [0,0,0];
	waitUntil {if(!isNull _nade)then{_nadePos = _nade modelToWorld [0,0,0]}; (isNull _nade || {_speedCheck && {vectorMagnitude (velocity _nade) == 0}})};
	if (_speedCheck && {alive _nade || {surfaceIsWater _nadePos && {_nadePos select 2 <= .1}}}) exitWith {};
	//sleep 1;
	for "_i" from 0 to (floor(random 15) + 15) step 1 do {
		_fragm = createVehicle [selectRandom CRS_fragm_type, _nadePos, [], 0, "can_collide"];
		_fragm setVectorDirAndUp [[
		random [-0.001,0,0.003],
		random [-0.001,0,0.005],
		random [0,0.0001,0.0002]],
		[random [0.001,0,0.005],
		random [0.001,0,0.001],
		random [0.001,0,0.001]]];
		_fragm setVelocity [random [-50,0,50], random [-50,0,50], random [1,8,12]];
		
		if (!isNil "hyp_var_tracer_tracedUnits" && {_unit in hyp_var_tracer_tracedUnits}) then {[[_unit,"","","","","",_fragm], position _fragm, vectorMagnitude (velocity _fragm)] spawn hyp_fnc_traceFireEvent};
		
		if (diag_fps > 29) then {
			_time = time + .015;
			waitUntil {time > _time};
		};
	};
}; //产生破片  "Sign_Sphere10cm_Geometry_F"

if(isNil "CRS_fragm_claymore")then {CRS_fragm_claymore = 99}; //破片数量
CRS_spawn_fragm2 = {
	params ["_unit", "_nade"];
	_dir = 0;
	_nadePos = _nade modelToWorld [0,0,0]; 
	_observer = createVehicle ["Sign_Sphere10cm_Geometry_F", _nadePos, [], 0, "can_collide"];
	if (surfaceIsWater _nadePos) then {_observer setPosASL _nadePos};
	_observer enableSimulation false;
	CRS_hide_simulation = _observer;
	publicVariableServer "CRS_hide_simulation";
	_observer setVariable ["CRS_observer_tag", _nade];
	_observer addEventHandler ["Explosion", {_observer = _this select 0; if (!alive (_observer getVariable "CRS_observer_tag")) then {_observer setVariable ["CRS_Explosion_tag", true]}}];
	
	waitUntil {if(alive _nade)then{_dir = getDir _nade; _nadePos = _nade modelToWorld [0,0,.15]}; sleep .2; !alive _nade};
	sleep .1; //
	if (!(_observer getVariable ["CRS_Explosion_tag", false]) && {!alive _nade}) exitWith {deleteVehicle _observer};
	deleteVehicle _observer;
	for "_i" from 0 to CRS_fragm_claymore do {
		_fragm = createVehicle ["B_338_NM_Ball", _nadePos, [], 0, "can_collide"];
		_dir2 = _dir + ([-30, 30] call BIS_fnc_randomInt); 
		_z = if(random 100 < 80)then[{random 7 + 1},{5 + random 45}];
		_fragm setVelocity [sin _dir2*(70 + random 70),cos _dir2*(70 + random 70),_z]; 
		
		if (!isNil "hyp_var_tracer_tracedUnits" && {_unit in hyp_var_tracer_tracedUnits}) then {[[_unit,"","","","","",_fragm], position _fragm, vectorMagnitude (velocity _fragm)] spawn hyp_fnc_traceFireEvent};
	};
};


CRS_STFUCarl_D = {player setVariable ["CRS_Speaker", speaker player]; enableSentences false; player disableConversation true; player setVariable ["BIS_noCoreConversations", true]; player setSpeaker"NoVoice"};
CRS_STFUCarl_E = {enableSentences true; player disableConversation false; player setVariable ["BIS_noCoreConversations", false]; player setSpeaker (player getVariable ["CRS_Speaker", speaker player])};
"CRS_STFUCarl" addPublicVariableEventHandler {call (if(CRS_STFUCarl)then[{CRS_STFUCarl_D},{CRS_STFUCarl_E}])};


CRS_AddKeyDown = {
	waitUntil {!isNull (findDisplay 46)};
	CRS_KeyDown = (findDisplay 46) displayAddEventHandler ["KeyDown", {
		if(inputAction "TacticalPing" > 0 && {isNull objectParent player} && {CRS_TacticalPoint})then{player playAction "HandSignalPoint"}; //战术手势
		if(inputAction "nextCM" > 0 && {isNull objectParent player} && {stance player == "STAND"})then{player switchMove "Acts_JetsMarshallingClear_in"}; //ctrl+c
	}]; //player playMove "AmovPercMstpSrasWrflDnon_AinvPercMstpSrasWrflDnon_Putdown"; //SHOULDER TAP
};
call CRS_AddKeyDown;


if (CRS_STFUCarl) then {call CRS_STFUCarl_D}; //个人角色语音

CRS_AddUnit = {
	_this setVariable ["CRS_tag_add", false];
	_idx = _this addEventHandler ["Fired", {
		_unit = _this select 0;
		if (local _unit && {count _this > 6}) then {
			if (CRS_BackBlast && {(_this select 1) isKindOf ["LauncherCore", configFile >> "CfgWeapons"]}) then {_this spawn CRS_BB}; //尾焰冲击 "Launcher"
			if (CRS_Fragmentation) then {
				if (_this select 5 in CRS_NadeList) then {[_unit, _this select 6, false] spawn CRS_spawn_fragm}; //破片手榴弹
				if (["HE_Grenade", _this select 5] call BIS_fnc_inString) then {[_unit, _this select 6, true] spawn CRS_spawn_fragm}; //高爆榴弹
				if (_this select 5 in ["ClaymoreDirectionalMine_Remote_Mag"]) then {[_unit, _this select 6] spawn CRS_spawn_fragm2}; //阔剑地雷
			};
			if (CRS_DisposableAT && {_this select 1 in ["launch_NLAW_F","CUP_launch_NLAW","CUP_launch_M136","CUP_launch_RPG18"]}) then {_this spawn CRS_AT}; //一次性火箭筒
		};
	}];
	_this setVariable ["CRS_Fired_eh", _idx];
};

CRS_AddVeh = {
	_this setVariable ["CRS_tag_add", false];
	_idx = _this addEventHandler ["Fired", {
		if (count _this > 7) then {
			_unit = _this select 7; //炮手
			if (local _unit) then {
				if (CRS_BackBlast && {(_this select 1) isKindOf ["LauncherCore", configFile >> "CfgWeapons"]}) then {_this spawn CRS_BB};
			};
		};
	}];
	_this setVariable ["CRS_Fired_eh", _idx];
};


//%1 for each enabled feature
if (isNil "CRS_DISABLE_Diary") then {
player createDiarySubject["CRS_Diary","CRS脚本"];
player createDiaryRecord["CRS_Diary",["CRS信息",
"<font face='PuristaMedium' size='27' shadow='5' color='#808000'>C O M B A T   R E A L I S M<br/>
S C R I P T S</font></size><br/>
by Phronk  k1升级版<br/>
<font face='PuristaMedium' size=12 color='#8E8E8E'>__________________________________</font></size><br/><br/>
    真实战斗脚本是一组游戏增强功能<br/><br/>
    这些脚本的目的是改进ARMA的步兵战斗<br/>
    通过使用直观的控制和设计来进行仿真，下面是<br/>
    本特派团通过CRS列入的特性清单:<br/><br/>
    • 一次性AT: PCML筒子只能使用一次<br/><br/>
    • 尾焰冲击: 发射器尾焰会冲击伤害后面的人<br/><br/>
    • 战术手势: 按 'TacticalPing' 战术标记键时用手指示前方<br/><br/>
    • 举手示意: ctrl+c（反制措施模式的默认按键）<br/><br/>
    • 破片效果: 防御型手雷，高爆榴弹，阔剑地雷在爆炸时产生破片<br/><br/>
    • 角色语音: 你的角色声音将被禁止 (游戏内置语音可以使用)<br/><br/><br/>
更多的功能还在未来..."]];
};
/*
CRS_check_mags = {
	private _check = false;
	{if (["_G_belt", _x] call BIS_fnc_inString) exitWith {_check = true}} forEach magazines _this;
	_check
};*/
CRS_check_weps = {
	private _check = false;
	{if (_x isKindOf ["LauncherCore", configFile >> "CfgWeapons"]) exitWith {_check = true}} forEach weapons _this;
	_check
};
while {true} do {
	{
		if (local _x && {alive vehicle _x} && {_x isKindOf "CaManBase"}) then {
			if (isNull objectParent _x) then [{
				if (_x getVariable ["CRS_tag_add", true]) then {_x call CRS_AddUnit};
			},{
				_veh = vehicle _x;
				if (_veh getVariable ["CRS_tag_add", true] && {_veh call CRS_check_weps}) then {_veh call CRS_AddVeh}; //_veh call CRS_check_mags
			}];
		};
	} forEach allUnits; //
	sleep 30;
};


